//
//  HEGoodsDetailsFooterView.m
//  HealthFamily
//
//  Created by LOOK on 2020/10/24.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEGoodsDetailsFooterView.h"
#import "HEHotRecomCell.h"
#import "ULBCollectionViewFlowLayout.h"

@interface HEGoodsDetailsFooterView ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray <HEGoodsModel *> * goodsArr;
@property (nonatomic, strong) ULBCollectionViewFlowLayout *flow;

@end

static NSString * const CellGoodsHotRecomId = @"CellGoodsHotRecomId";
static NSString * const SupplementaryViewHeaderIdentify = @"SupplementaryViewHeaderIdentify";


@implementation HEGoodsDetailsFooterView

- (NSMutableArray<HEGoodsModel *> *)goodsArr {
    if (!_goodsArr) {
        _goodsArr = [NSMutableArray array];
    }
    return _goodsArr;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initConllectionView];
        [self loadGoodsData];
    }
    return self;
}
- (void)loadGoodsData {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:[NSNumber numberWithInteger:1] forKey:@"pageNum"];
    [params setObject:[NSNumber numberWithInteger:10] forKey:@"pageSize"];
    [params setObject:[NSNumber numberWithInteger:1] forKey:@"recommandStatus"];

    [WOAFNetWorkingHandle GETWithBaseUrl:KServerUrl suffixUrl:KGoodsDetailsUserGood parameters:params success:^(id  _Nonnull responseObject) {
        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        [self.goodsArr addObjectsFromArray:[HEGoodsModel mj_objectArrayWithKeyValuesArray:response.data]];
        [self.collectionView reloadData];

        // 返回collectionView真实高度
        if (self.heightBlock) {
            self.heightBlock(self.flow.collectionViewContentSize.height);
        }

     } failure:^(NSError * _Nonnull error) {
    }];
}

- (void)getFooterViewHeightBlock:(getViewHeightBlock)block {
    _heightBlock = block;
}

- (void)didSelectItemAtFooterView:(didSelectAtFooterView)block {
    _selectItemBlock = block;
}

- (void)initConllectionView {

    ULBCollectionViewFlowLayout *flow = [[ULBCollectionViewFlowLayout alloc] init];
    _flow = flow;
    flow.minimumLineSpacing = 15; // 行间距
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flow];
    [_collectionView startInteractiveTransitionToCollectionViewLayout:flow completion:^(BOOL completed, BOOL finished) {
        
    }];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    if (@available(iOS 11.0, *)) {
        _collectionView.contentInsetAdjustmentBehavior = NO;
    } else {
        // Fallback on earlier versions
    }
    _collectionView.scrollEnabled = NO;
    [self addSubview:_collectionView];
    
    
//    _collectionView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self);
    }];
    
//    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
//    header.automaticallyChangeAlpha = YES;
//    header.lastUpdatedTimeLabel.hidden = YES;
//    header.stateLabel.hidden = YES;
//    _collectionView.mj_header = header;

        //注册
    //    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellReuseIdentify];
    [_collectionView registerNib:[UINib nibWithNibName:@"HEHotRecomCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:CellGoodsHotRecomId];
    //UICollectionElementKindSectionHeader注册是固定的
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SupplementaryViewHeaderIdentify];

}


#pragma mark - UICollectionViewDataSource method
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.goodsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HEHotRecomCell *cell = (HEHotRecomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellGoodsHotRecomId forIndexPath:indexPath];
    cell.model = self.goodsArr[indexPath.item];
    return cell;
}

 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
     if([kind isEqualToString:UICollectionElementKindSectionHeader]){
         UICollectionReusableView *supplementaryView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SupplementaryViewHeaderIdentify forIndexPath:indexPath];
  
         // 热门推荐
         UILabel *recomLb = [UILabel new];
         recomLb.text = @"猜您喜欢";
         recomLb.font = DYBlodFont(18);
         [supplementaryView addSubview:recomLb];
         recomLb.frame = CGRectMake(15, 15, KScreenWidth, 30);
//         [recomLb mas_makeConstraints:^(MASConstraintMaker *make) {
//             make.top.equalTo(supplementaryView).mas_offset(15);
//             make.left.equalTo(supplementaryView).mas_offset(15);
//         }];
        
         return supplementaryView;
     }
     else if([kind isEqualToString:UICollectionElementKindSectionFooter]) {
 //        UICollectionReusableView *supplementaryView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:SupplementaryViewFooterIdentify forIndexPath:indexPath];
 //        supplementaryView.backgroundColor = [UIColor redColor];
 //        return supplementaryView;
     }
     return nil;
 }

#pragma mark - UICollectionViewDelegate method
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
 
}

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
 
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_selectItemBlock) {
        _selectItemBlock(self.goodsArr[indexPath.item]);
    }
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    if ([NSStringFromSelector(action) isEqualToString:@"copy:"]
        || [NSStringFromSelector(action) isEqualToString:@"paste:"])
        return YES;
    return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    NSLog(@"复制之后，可以插入一个新的cell");
}


#pragma mark - UICollectionViewDelegateFlowLayout method
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(KScreenWidth, 40);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15, 15, 15, 15);//分别为上、左、下、右
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((KScreenWidth-45)/2.0, 246);
    
}
- (UIColor *)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout colorForSectionAtIndex:(NSInteger)section {
    return KHexColor(@"F8F8F8");
}

@end
